package com.objetdirect.gwt.umldrawer.client;

import java.util.Date;

import com.gwtext.client.core.EventObject;
import com.gwtext.client.core.RegionPosition;
import com.gwtext.client.data.ArrayReader;
import com.gwtext.client.data.DateFieldDef;
import com.gwtext.client.data.FieldDef;
import com.gwtext.client.data.MemoryProxy;
import com.gwtext.client.data.RecordDef;
import com.gwtext.client.data.Store;
import com.gwtext.client.data.StringFieldDef;
import com.gwtext.client.widgets.Button;
import com.gwtext.client.widgets.Panel;
import com.gwtext.client.widgets.Toolbar;
import com.gwtext.client.widgets.ToolbarButton;
import com.gwtext.client.widgets.event.ButtonListenerAdapter;
import com.gwtext.client.widgets.grid.BaseColumnConfig;
import com.gwtext.client.widgets.grid.CheckboxColumnConfig;
import com.gwtext.client.widgets.grid.CheckboxSelectionModel;
import com.gwtext.client.widgets.grid.ColumnConfig;
import com.gwtext.client.widgets.grid.ColumnModel;
import com.gwtext.client.widgets.grid.GridPanel;
import com.gwtext.client.widgets.layout.BorderLayout;
import com.gwtext.client.widgets.layout.BorderLayoutData;
import com.gwtext.client.widgets.layout.FitLayout;

public class InboxPage {
	
	private Panel inboxPanel = new Panel();
	private Panel messagesPanel = new Panel();
	private Panel msgContentPanel = new Panel();

	public Panel getInbox() {

		inboxPanel.setSize(920, 500);
		inboxPanel.setTitle("Inbox");
		inboxPanel.setCollapsible(true);
		inboxPanel.setTitleCollapse(true);
		inboxPanel.setStyleName("inboxPanel");
		inboxPanel.setLayout(new FitLayout());

		Panel borderPanel = new Panel();
		borderPanel.setLayout(new BorderLayout());

		borderPanel.add(messagesPanel, new BorderLayoutData(RegionPosition.CENTER));
		
		/*
		 * Grid Definition
		 */
		final CheckboxSelectionModel cbSelectionModel = new CheckboxSelectionModel();
		RecordDef recordDef = new RecordDef(new FieldDef[] {
				new StringFieldDef("from"), new StringFieldDef("subject"),
				new DateFieldDef("date") });

		GridPanel inboxGrid = new GridPanel();

		Object[][] data = getCompanyData();
		MemoryProxy proxy = new MemoryProxy(data);

		ArrayReader reader = new ArrayReader(recordDef);
		Store store = new Store(proxy, reader);
		store.load();
		inboxGrid.setStore(store);

		BaseColumnConfig[] columns = new BaseColumnConfig[] {
				new CheckboxColumnConfig(cbSelectionModel),
				new ColumnConfig("FROM", "from", 305),
				new ColumnConfig("SUBJECT", "subject", 284),
				new ColumnConfig("DATE", "date", 300, true, null, "date") };

		ColumnModel columnModel = new ColumnModel(columns);
		inboxGrid.setColumnModel(columnModel);

		inboxGrid.setSelectionModel(cbSelectionModel);
		inboxGrid.setWidth(914);
		inboxGrid.setAutoScroll(true);
		
		messagesPanel.add(inboxGrid);
		messagesPanel.setAutoScroll(true);
		
		/*
		 * ToolBar for messages panel
		 */
		Toolbar inboxToolbar = new Toolbar();

		ToolbarButton openMessage = new ToolbarButton("Open");
		openMessage.setIcon("images/openMessage.png");
		inboxToolbar.addButton(openMessage);
		
		ButtonListenerAdapter openMessageAdapter = new ButtonListenerAdapter() {
			@Override
			public void onClick(Button button, EventObject e) {
			}
		};
		openMessage.addListener(openMessageAdapter);
		
		ToolbarButton deleteMessage = new ToolbarButton("Delete");
		deleteMessage.setIcon("images/deleteMessage.png");
		inboxToolbar.addButton(deleteMessage);

		ButtonListenerAdapter deleteMessageAdapter = new ButtonListenerAdapter() {
			@Override
			public void onClick(Button button, EventObject e) {
			}
		};
		deleteMessage.addListener(deleteMessageAdapter);
		
		inboxToolbar.addSeparator();
		
		ToolbarButton reply = new ToolbarButton("Reply");
		reply.setIcon("images/replyMessage.png");
		inboxToolbar.addButton(reply);
		
		ButtonListenerAdapter replyAdapter = new ButtonListenerAdapter() {
			@Override
			public void onClick(Button button, EventObject e) {
			}
		};
		reply.addListener(replyAdapter);
		
		ToolbarButton forward = new ToolbarButton("Forward");
		forward.setIcon("images/forwardMessage.png");
		inboxToolbar.addButton(forward);
		
		ButtonListenerAdapter forwardAdapter = new ButtonListenerAdapter() {
			@Override
			public void onClick(Button button, EventObject e) {
			}
		};
		forward.addListener(forwardAdapter);
		
		inboxToolbar.addSeparator();
		
		ToolbarButton markAsRead = new ToolbarButton("Mark As Read");
		markAsRead.setIcon("images/markAsRead.png");
		inboxToolbar.addButton(markAsRead);

		ButtonListenerAdapter markAsReadAdapter = new ButtonListenerAdapter() {
			@Override
			public void onClick(Button button, EventObject e) {
			}
		};
		markAsRead.addListener(markAsReadAdapter);

		ToolbarButton markAsUnRead = new ToolbarButton("Mark As Un-Read");
		markAsUnRead.setIcon("images/markAsUnRead.png");
		inboxToolbar.addButton(markAsUnRead);

		ButtonListenerAdapter markAsUnReadAdapter = new ButtonListenerAdapter() {
			@Override
			public void onClick(Button button, EventObject e) {
			}
		};
		markAsUnRead.addListener(markAsUnReadAdapter);
		
		inboxToolbar.addSeparator();
		
		ToolbarButton printMessage = new ToolbarButton("Print Message");
		printMessage.setIcon("images/printMessage.png");
		inboxToolbar.addButton(printMessage);
		
		ButtonListenerAdapter printMessageAdapter = new ButtonListenerAdapter() {
			@Override
			public void onClick(Button button, EventObject e) {
			}
		};
		printMessage.addListener(printMessageAdapter);

		inboxToolbar.addSeparator();
		
		ToolbarButton refresh = new ToolbarButton("Refresh");
		refresh.setIcon("images/refreshInbox.png");
		inboxToolbar.addButton(refresh);
		
		ButtonListenerAdapter refreshAdapter = new ButtonListenerAdapter() {
			@Override
			public void onClick(Button button, EventObject e) {
			}
		};
		refresh.addListener(refreshAdapter);
		
		inboxToolbar.addSeparator();
		
		ToolbarButton emptyInbox = new ToolbarButton("Empty My Inbox");
		emptyInbox.setIcon("images/emptyInbox.png");
		inboxToolbar.addButton(emptyInbox);
		
		ButtonListenerAdapter emptyInboxAdapter = new ButtonListenerAdapter() {
			@Override
			public void onClick(Button button, EventObject e) {
			}
		};
		emptyInbox.addListener(emptyInboxAdapter);
		
		inboxToolbar.addSeparator();
		
		ToolbarButton composeMessage = new ToolbarButton("Compose Message");
		composeMessage.setIcon("images/composeMessage.png");
		inboxToolbar.addButton(composeMessage);

		ButtonListenerAdapter composeMessageAdapter = new ButtonListenerAdapter() {
			@Override
			public void onClick(Button button, EventObject e) {
				new MessageFormWindow().showMessageFormWindow();
			}
		};
		composeMessage.addListener(composeMessageAdapter);

		inboxPanel.setTopToolbar(inboxToolbar);
		
		// add message content panel
		msgContentPanel.setHtml("Message Contnet ... ");
		msgContentPanel.setHeight(150);
		msgContentPanel.setCollapsible(true);
		msgContentPanel.setTitle("Message Content");
		msgContentPanel.setBodyBorder(true);
		
		BorderLayoutData southData = new BorderLayoutData(RegionPosition.SOUTH);
		southData.setMinSize(100);
		southData.setMaxSize(200);
		southData.setSplit(true);
		borderPanel.add(msgContentPanel, southData);
		
		inboxPanel.add(borderPanel);
		return inboxPanel;
	}
	
	private Object[][] getCompanyData() {
		return new Object[][] {
				new Object[] { "Sandra Sabry", "Subject", new Date() }};
	}
}
